<!-- 表格 -->
<template>
  <div>
    <el-table :data="tableData"
              :border="tableConf.border"
              :stripe="tableConf.stripe"
              :header-cell-style="{background:`${tableConf.color}`,width: '100%'}">
      <template v-for="(item, index) in columns">
        <!-- :type="item.type == 'selection' ? 'selection' : ''" -->
        <el-table-column v-if="item.show != false"
                         :key="index"
                         :label="item.label"
                         :align="'center'"
                         :fixed="item.fixed?item.fixed:false"
                         :width="item.width">
          <template slot-scope="scope">
            <div class="row-style "
                 v-if="scope.row[item.prop]==='' ">
            </div>
            <template v-else>
              <!-- <div class="row-style sort-index"
                   v-if="item.type=='selection'">
                <el-checkbox v-model="scope.row[item.prop]"/>
              </div> -->

              <!-- 序号 -->
              <div class="row-style sort-index"
                   v-if="item.type=='sort'">{{scope.$index + 1}}</div>
              <!-- 图片 -->
              <el-popover class="item"
                          effect="light"
                          placement="top"
                          v-else-if="item.type=='img'">
                <div class="tooltip-box">
                  <template v-if="typeof scope.row[item.prop] == 'string'">
                    <img :src="scope.row[item.prop]">
                  </template>
                  <template v-else>
                    <img v-for="(img , i) in scope.row[item.prop]"
                         :key="i"
                         :src="img">
                  </template>
                </div>
                <div class="row-img"
                     slot="reference">
                  <template v-if="typeof scope.row[item.prop] == 'string'">
                    <img :src="scope.row[item.prop]">
                  </template>
                  <template v-else>
                    <img v-for="(img , i) in scope.row[item.prop]"
                         :key="i"
                         :src="img">
                  </template>
                </div>
              </el-popover>
              <!-- 开关 -->
              <div class="row-style sort-index"
                   v-else-if="item.type=='switch'">
                <template v-if="item.data.isShow !== scope.row[item.prop]">
                  <el-switch v-model="scope.row[item.prop]"
                             @change="item.data.change(scope.row ,$event)"
                             :active-value="item.data.active"
                             :inactive-value="item.data.inactive"
                             active-color="#13ce66"
                             inactive-color="#d2d2d2">
                  </el-switch>
                  <span class="switch-text">
                    {{ scope.row[item.prop] == item.data.active ? item.data.activeText : item.data.inactiveText}}
                  </span>
                </template>
              </div>
              <!-- 文本 -->
              <div class="row-style "
                   v-else-if="item.type=='text'">
                <span v-for="(itemText ,indText) in item.data"
                      :key="indText"
                      :style="{color: itemText.color? itemText.color : '#333' }"
                      v-show="itemText.type == scope.row[item.prop]">{{itemText.value }}</span>
              </div>

              <!-- 文本 -金额 -->
              <div class="row-style "
                   v-else-if="item.type=='money'">
                <div v-html="toMoney(scope.row[item.prop])"
                     class="tooltip-box"></div>
              </div>

              <!-- 输入标签 -->
              <div class="row-style "
                   v-else-if="item.type=='temp'">
                <el-input v-model="scope.row[item.prop]"
                          size="small"
                          @blur="item.data.fun(scope.row)"
                          :placeholder="item.data.placeholder? item.data.placeholder : '请输入内容' "></el-input>
              </div>
              <!-- 模板 -->
              <el-popover class="item"
                          effect="light"
                          placement="top"
                          v-else-if="item.type=='model'">
                <div v-html="item.temp(scope.row)"
                     class="tooltip-box"></div>
                <div v-html="item.temp(scope.row)"
                     class="row-style"
                     slot="reference"
                     :style="{justifyContent:(item.align? item.align=='right'? 'flex-end': 'center' :'flex-start')}"></div>
              </el-popover>
              <!-- 操作 v-has="itemBtn.has"-->
              <div class="row-style "
                   v-else-if="item.type=='edit'">
                <el-row>
                  <span v-for="(itemBtn ,btnid) in item.data.btn"
                        :key="btnid">

                    <el-button :style="{'background-color': itemBtn.bgcolor, 'color':itemBtn.color}"
                               size="mini"
                               class="btn-box"
                               @click="itemBtn.click(scope.row)"
                               v-if=" typeof itemBtn.has == 'undefined'">
                      {{ itemBtn.text}}
                    </el-button>
                    <el-button :style="{'background-color': itemBtn.bgcolor, 'color':itemBtn.color}"
                               size="mini"
                               class="btn-box"
                               v-else
                               v-has="itemBtn.has"
                               @click="itemBtn.click(scope.row)">
                      {{ itemBtn.text}}
                    </el-button>
                  </span>
                </el-row>
              </div>
              <!-- 内容 -->
              <el-popover class="item"
                          effect="light"
                          placement="top"
                          v-else>
                <div v-html="scope.row[item.prop]"
                     class="tooltip-box"></div>
                <div v-html="scope.row[item.prop]"
                     class="row-style"
                     slot="reference"
                     :style="{justifyContent:(item.align?  item.align=='right'? 'flex-end': 'center' :'flex-start')}"></div>
              </el-popover>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="page-box">
      <el-pagination v-if="pagin"
                     @size-change="handleSizeChange"
                     @current-change="currentchange"
                     :page-sizes="[10, 20, 30, 40]"
                     :page-size="pagin.limit"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="pagin.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { Table, TableColumn, Pagination, popover, row, button, Checkbox, Loading } from 'element-ui';

export default {
  name: "commonTable",
  components: {
    [Table.name]: Table,
    [TableColumn.name]: TableColumn,
    [Pagination.name]: Pagination,
    [popover.name]: popover,
    [row.name]: row,
    [button.name]: button,
    [Checkbox.name]: Checkbox,
    [Loading.name]: Loading,

  },
  props: {
    columns: Array,
    tableData: Array,
    pagin: Object,
  },
  data() {
    return {
      tableConf: {
        border: true,
        stripe: true,
        color: "#e6e6e6"
      },
    };
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("tableSize", val);
    },
    currentchange(val) {
      this.$emit("tablePage", val);
    },
    toMoney(num) {
      if (num) {
        if (isNaN(num)) {
          // alert("金额中含有不能识别的字符");
          return num;
        }
        num = typeof num == "string" ? parseFloat(num) : num; //判断是否是字符串如果是字符串转成数字
        num = num.toFixed(2); //保留两位
        num = parseFloat(num); //转成数字
        num = num.toLocaleString(); //转成金额显示模式
        //判断是否有小数
        if (num.indexOf(".") == -1) {
          num = "￥" + num + ".00";
        } else {
          num = num.split(".")[1].length < 2 ? "￥" + num + "0" : "￥" + num;
        }
        return num;
      } else {
        return (num = num);
      }
    },
  }
};
</script>

<style  scoped>
.page-box {
  display: block;
  padding: 5px 0;
  border-right: 1px solid #ebeef5;
  border-left: 1px solid #ebeef5;
  border-bottom: 1px solid #ebeef5;
}

.row-style {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  min-height: 25px;
  max-height: 50px;
  overflow-y: hidden;
  overflow-x: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.row-img {
  display: block;
  width: 100%;
  height: 50px;
  text-align: center;
}
.row-img img {
  display: inline-block;
  height: 100%;
}

.tooltip-box {
  display: block;
  max-width: 500px;
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;
}
.tooltip-box >>> p {
  width: 100%;
}
.tooltip-box >>> p > img {
  width: 100%;
}

.tooltip-box >>> img {
  width: 100%;
}
.sort-index {
  justify-content: center;
}
/* 开关文字 */
.switch-text {
  display: block;
  padding: 0 5px;
}

.btn-box {
  padding: 5px;
}
</style>